// 导入api接口
import { cpList, delImgCar, listDept, listTeam, vehicleBasicInfoAdd, vehicleBasicInfoCartype, vehicleBasicInfoDelete, vehicleBasicInfoDownExl, vehicleBasicInfoImgList, vehicleBasicInfoList, vehicleBasicInfoUpdate } from '@/api/api'
import { dateRevert, imgFilter } from '@/utils/businessTools'
import configJs from '@/utils/configJs'
import defaultData from './data'
var data = JSON.parse(JSON.stringify(defaultData))
var adapter = {
	data,
	methods: {
		// ...mapActions(['logOut']),
		/**
		 * 初始化
		 */
		initPage(){
			//还原页面对象
			configJs.revert(data, defaultData);
			this.carSearchObj[0].zxcph = this.$store.state.app.navcp; // 写入公共车牌号
		},
		
		/**
		 * 查询车辆列表
		 */
		async search(val) {
			if (!this.$store.getters.pageAuth.authoritySearch) {
				return
			}
			this.revertActive() // 重置选择行
			let params = {...this.carSearchObj[0]}
			// 处理时间
			params.jqxyxjzrq = dateRevert(params.jqxyxjzrq)
			params.zcrq = dateRevert(params.zcrq)
			params.syxyxjzrq = dateRevert(params.syxyxjzrq)
			params.whxyxjzrq = dateRevert(params.whxyxjzrq)
			typeof val != 'number' && (val = 1)
			params.page = val
			const res = await vehicleBasicInfoList(params)
			if (res.code === 0) {
			  this.page = res.data
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 刷新
		 */
		async refresh() {
			this.carSearchObj = [{
				zxcph:'',
				cllx:'',
				csys:'',
				zcrq:['',''],
				jqxyxjzrq:['',''],
				syxyxjzrq:['',''],
				whxyxjzrq:['',''],
				lscph:''
			}]
			this.search()
		},

		/**
		 * 下载excel
		 */
		async downloadExl() {
			vehicleBasicInfoDownExl()
		},

		/**
		 * 增加
		 */
		add() {
			this.resetAdd()
			this.addVisi = true
		},

		/**
		 * 重置增加弹窗
		 */
		resetAdd() {
			this.addCar = [{
				zxcph:'',
				cllx:'',
				csys:'',
				zcrq:'',
				jqxyxjzrq:'',
				syxyxjzrq:'',
				whxyxjzrq:'',
				lscph:''
			}]
		},

		/**
		 * 增加点击
		 */
		async addClick() {
			const res = await vehicleBasicInfoAdd(this.addCar[0])
			if (res.code === 0) {
				this.$message('添加成功')
				this.addVisi = false
				this.refresh()
			} else {
				this.$message(res.msg)
			}
		},

		 /**
			* 修改
		  */
		edit(car) {
			console.log(car)
			console.log(this.editCar)
			this.editCar = {...car}
			this.editVisi = true
		},

		/**
		 * 日期转换
		 */
		dateFormat(date) {
			if ('' === date) {
				return date
			} else {
				if (typeof date == 'String') {
					let arr = date.split('_')
					return arr
				} else {
					if (Array.isArray(date) && date.length == 2) {
						return date[0] + '_' + date[1]
					} else {
						return date
					}
				}
			}
		},

		/**
		 * 修改点击
		 */
		async editClick() {
			console.log(this.editCar)
			const res = await vehicleBasicInfoUpdate(this.editCar)
			if (res.code === 0) {
				this.$message('修改成功')
				this.editVisi = false
				this.refresh()
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 删除
		 */
		async deleteClick(id) {
			await this.$confirm('确认删除?', '', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
			const res = await vehicleBasicInfoDelete(id)
			if (res.code === 0) {
				this.$message('修改成功')
				this.editVisi = false
				this.refresh()
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 点击行
		 */
		rowClick(row, column, e) {
			this.activeCar = row
			this.getImgList()
		},

		/**
		 * 重置选择行
		 */
		revertActive() {
			this.activeCar = {"id":null,"zxcph":"","cllx":"","csys":null,"zcrq":null,"jqxyxjzrq":null,"syxyxjzrq":null,"whxyxjzrq":null,"lscph":null,"bz":null,"dabh":null,"zcrq_2":null,"nsrq":null,"syxz":null,"qzbfrq":null,"bfnx":null,"dlyszh":null,"fzrq":null,"nsrq_2":null,"yszyxq":null,"xhxz":null,"syxz_2":null,"jyfw":null,"cp":null,"zzcm":null,"ccrq":null,"gcjk":null,"clxh":null,"vinhcjh":null,"dpxh":null,"clcc":null,"hxlcchrj":null,"zzl":null,"zbzl":null,"hdzzl":null,"zqyzl":null,"dw":null,"hdzk":null,"fdjhm":null,"fdjxh":null,"fdjgl":null,"fdjpl":null,"pfbz":null,"dclx":null,"qddjxh":null,"qddjgl":null,"dllx":null,"czsl":null,"zj":null,"qlj":null,"hlj":null,"gbthps":null,"lts":null,"ltgg":null,"xczdfs":null,"zdqxs":null,"ywabs":null,"bsqxs":null,"hsq":null,"ktxt":null,"wxdwzz":null,
			"bz_2":null}
			this.activeImgList = []
		},

		/**
		 * 日期选择
		 */
		 datePick() {
			 console.error('datepick')
			 console.log(arguments)
			 return 1
		 },

		/**
		 * 部门列表
		 */
		async getlistDept() {
			const res = await listDept()
			if (res.code === 0) {
			  this.deptList = res.data.list
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 车辆类型列表
		 */
		async getListCartype() {
			const res = await vehicleBasicInfoCartype()
			if (res.code === 0) {
			  this.cartypeList = res.data.list
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 小组列表
		 */
		async getlistTeam() {
			const res = await listTeam()
			if (res.code === 0) {
			  this.teamList = res.data.list
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 车牌列表
		 */
		 async getListCp() {
			const res = await cpList()
			if (res.code === 0) {
			  this.cpList = res.data.list
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 获取图片列表
		 */
		async getImgList() {
			const res = await vehicleBasicInfoImgList(this.activeCar.id)
			if (res.code === 0) {
				this.activeImgList = imgFilter(res.data.list)
			}
		},

		/**
		 * 删除图片
		 */
		async imgDel(fileName) {
			await this.$confirm('确认删除?', '', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
			const res = await delImgCar(fileName)
			if (res.code === 0) {
				this.$message('删除成功')
				this.getImgList()
			} else {
				this.$message(res.msg)
			}
		}
	},
	computed: {
	}
}
export default adapter
